package cn.lg.soar.boot.nc.service.impl;

import cn.lg.soar.boot.nc.entity.vo.GuaranteeVo;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.IPageQuery;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.common.util.ParameterUtil;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.database.query.SqueryWapper;
import cn.lg.soar.database.util.PageUtils;
import cn.lg.soar.system.api.service.IUserApi;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.lg.soar.database.base.service.impl.BaseServiceImpl;
import cn.lg.soar.boot.nc.mapper.GuaranteeMapper;
import cn.lg.soar.boot.nc.entity.Guarantee;
import cn.lg.soar.boot.nc.service.GuaranteeService;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 服务保障
 * @author ZhuanZ（无密码）
 * @since 2025-02-17
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Service
public class GuaranteeServiceImpl extends BaseServiceImpl<GuaranteeMapper, Guarantee> implements GuaranteeService {

    @Autowired
    IUserApi iUserApi;

    private SqueryWapper<Guarantee> guaranteeSqueryWapper;

    @Override
    public HttpResult<Serializable> updateSort(List<Map<String, Integer>> sortedData) {
        if (null == sortedData || 0 >= sortedData.size()) {
            return HttpResult.fail("4000", "排序数据不可为空", "error");
        }

        sortedData.forEach(item-> {

            if (!item.containsKey("id") || !item.containsKey("orders")) {
                return;
            }

            Long id = item.get("id").longValue();
            int orders = item.get("orders");

            Guarantee guarantee = new Guarantee();
            guarantee.setId(id);
            guarantee.setOrders(orders);
            this.updateById(guarantee);
        });

        return HttpResult.success("排序成功");
    }

    @Override
    public HttpResult<PageResult<GuaranteeVo>> getGuaranteeVo(PageQuery pageQuery, QueryParams params) {
        params.setFields(new ArrayList<>());//查询字段置为空，默认查询所有

        PageResult<GuaranteeVo> resultVo = new PageResult<>();

        if (guaranteeSqueryWapper==null) {
            guaranteeSqueryWapper = new SqueryWapper<>(Guarantee.class);
        }
        QueryWrapper<Guarantee> guaranteeQueryWrapper = Wrappers.query();
        if (pageQuery==null || pageQuery.getPageSize()<=0) {
            guaranteeSqueryWapper.setWhere(params.getF(), guaranteeQueryWrapper);
            guaranteeSqueryWapper.setSelect(params.getFields(), guaranteeQueryWrapper);
            guaranteeSqueryWapper.setOrderBy(params.getOrderBy(), guaranteeQueryWrapper);
            //所有数据
            List<Guarantee> list = super.list(guaranteeQueryWrapper);
            //存储数据
            List<GuaranteeVo> voList = new ArrayList<>();

            for (Guarantee guarantee : list) {
                GuaranteeVo vo = new GuaranteeVo();
                BeanUtils.copyProperties(guarantee, vo);
                //获取用户,并塞入模板
                vo.setUpdateBy(iUserApi.getById(guarantee.getUpdateBy()).getNickname());
                voList.add(vo);
            }
            //塞入模型数据
            resultVo.setList(voList);
            return  HttpResult.data(resultVo);
        }
        resultVo.setPageNo(pageQuery.getPageNo());
        resultVo.setPageSize(pageQuery.getPageSize());
        resultVo.setQueryType(pageQuery.getQueryType());

        if (IPageQuery.QueryType.total.equals(pageQuery.getQueryType())) {
            guaranteeSqueryWapper.setWhere(params.getF(), guaranteeQueryWrapper);
            resultVo.setTotal(super.count(guaranteeQueryWrapper));
        }
        IPage<Guarantee> page = PageUtils.toPage(pageQuery);
        guaranteeSqueryWapper.setWhere(params.getF(), guaranteeQueryWrapper);
        guaranteeSqueryWapper.setSelect(params.getFields(), guaranteeQueryWrapper);
        guaranteeSqueryWapper.setOrderBy(params.getOrderBy(), guaranteeQueryWrapper);

        page = super.page(page, guaranteeQueryWrapper);
        resultVo.setTotal(page.getTotal());

        //存储数据
        List<GuaranteeVo> voList = new ArrayList<>();
        List<Guarantee> records = page.getRecords();
        for (Guarantee record : records) {
            GuaranteeVo vo = new GuaranteeVo();
            BeanUtils.copyProperties(record, vo);
            //获取用户,并塞入模板
            vo.setUpdateBy(iUserApi.getById(record.getUpdateBy()).getNickname());
            voList.add(vo);
        }
        //塞入模型数据
        resultVo.setList(voList);

        return HttpResult.data(resultVo);
    }

    @Override
    public HttpResult<GuaranteeVo> getById(Long id) {
        ParameterUtil.notNull(id, "缺少数据id");
        //返回数据
        GuaranteeVo vo = new GuaranteeVo();
        //原表数据
        Guarantee guarantee = super.getById(id);
        //复制数据
        BeanUtils.copyProperties(guarantee, vo);
        vo.setUpdateBy(iUserApi.getById(guarantee.getUpdateBy()).getNickname());

        return HttpResult.data(vo);
    }
}
